package com.example.bootintegration.actuator;

import com.example.bootintegration.service.ServiceA;
import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicLong;

@Service
@ManagedResource
public class TacoCounterMBean extends AbstractRepositoryEventListener<ServiceA> {

  private AtomicLong counter;

  public TacoCounterMBean() {
    long initialCount = 0L;
    this.counter = new AtomicLong(initialCount);
  }

  @Override
  protected void onAfterCreate(ServiceA entity) {
    counter.incrementAndGet();
  }

  @ManagedAttribute
  public long getTacoCount() {
    return counter.get();
  }

  @ManagedOperation
  public long increment(long delta) {
    return counter.addAndGet(delta);
  }
}